#ifndef ICALENDAR_H
#define ICALENDAR_H

#include "ics_types.h"
#include<cstring>
#include<functional>
namespace icslib {

typedef void(*fDataItemInfo)(icslib::Event *pData);

class ICalendar
{
public:
    ICalendar(const string &strPathName)
        : m_strFileName(strPathName)
        {

    }

    ~ICalendar() ;

    bool LoadFileData();
    void SetDataItemCb( std::function<void(icslib::Event *)> &f);

    class Query;
private:
    string GetProperty(const string &sLine) const {
        // if VALUE=DATE or VALUE=DATE-TIME used, then the date is not directly
        // after the property name so we just search for the string after ':'
        string sTemp = sLine.substr(sLine.find_first_of(':') + 1);
        unsigned int length = sTemp.length();
        if(length > 0 && sTemp[length -1] == '\r')
            sTemp.resize(length-1);
        return sTemp;
    }

    string GetSubProperty(const string &sLine, const char *subProperty) const {

        size_t pos = sLine.find(subProperty);
        if(pos == string::npos) {
            return "";
        }
        pos += strlen(subProperty) + 1;

        return sLine.substr(pos, sLine.find_first_of(";\r", pos) - pos);
    }

    void FixLineEnd(string &sLine, unsigned int length) {
        if(length > 0 && sLine[length-1] == '\r') {
            sLine.resize(length-1);
        }
        sLine += "\r\n";
    }


private:
    string m_strFileName;

    list <Event *> m_pListEevent;
    std::function<void(icslib::Event *)> callback_;
    /*fDataItemInfo m_fDataItemInfo*/;

};

class ICalendar::Query {

};

inline TimeUnit ConvertFrequency(string sName) {
    if (sName == "SECONDLY")
        return TimeUnit::kSecond;
    if (sName == "MINUTELY")
        return TimeUnit::kMinute;
    if (sName == "HOURLY")
        return TimeUnit::kHour;
    if (sName == "DAILY")
        return TimeUnit::kDay;
    if (sName == "WEEKLY")
        return TimeUnit::kWeek;
    if (sName == "MONTHLY")
        return TimeUnit::kMonth;

    return TimeUnit::kYear;
}

}



#endif // ICALENDAR_H
